﻿Imports FinanceiroBLL
Imports FinanceiroDAL.ModuloConexao
Public Class FormaPagamentoUC
    Dim objBLL As New FormasPgtoBLL

    Sub CarregaGrid()
        Dim dt As New DataTable
        dt = objBLL.localizaGeral()
        DGVDados.DataSource = dt
        DGVDados.Refresh()
    End Sub
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        statusfinanceiro = "N"
        Dispose()
    End Sub
#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        Dim fpagamento As New FormaPagamentoCAD
        TSBCadastrar.BackColor = Color.Gainsboro
        fpagamento.ShowDialog()
        CarregaGrid()
        TSBCadastrar.BackColor = Color.Transparent
    End Sub
#End Region
#Region "Load"
    Private Sub FormaPagamentoUC_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CBFiltro.SelectedIndex = 1
        TXTLocalizar.Text = ""
        PLocalizar.Visible = False
        'Determinar o começo o filtro como descrição
        CBFiltro.Focus()
        CarregaGrid()
    End Sub
#End Region
#Region "Evento Text Box"
    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        'If (CBFiltro.Text = "Descrição") Then
        '    DGVDados.DataSource = objBLL.localizarDescricao(TXTLocalizar.Text)
        'End If
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Descrição"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Status"
                DGVDados.DataSource = objBLL.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)

        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBLL.buscar("", 4)
        End If
    End Sub

#End Region
#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Try
            Dim cad As New FormaPagamentoCAD
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("pag_codigo") Is Nothing) Then
                    objBLL = New FormasPgtoBLL(Val(DGVDados.Rows(linha.Index).Cells("pag_codigo").Value.ToString()))
                End If
            Next
            If (objBLL.pagcodigo > 0) Then
                cad.lcodigo.Text = objBLL.pagcodigo
                cad.lcodigo.Visible = False
                cad.LTitulo.Text = objBLL.pagdescricao
                cad.Label1.Visible = True
                TSBAlterar.BackColor = Color.Gainsboro
                cad.ShowDialog()
                CarregaGrid()
                TSBAlterar.BackColor = Color.Transparent
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Erro:", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally
            TSBAlterar.BackColor = Color.Transparent
        End Try
    End Sub

#End Region
#Region "Buscar"
    Private Sub TSBBuscar_Click(sender As System.Object, e As System.EventArgs) Handles TSBBuscar.Click
        If (PLocalizar.Visible) Then
            PLocalizar.Visible = False
            CBFiltro.Visible = True
            TSBBuscar.BackColor = Color.Transparent
            CarregaGrid()
        Else
            PLocalizar.Visible = True
            TSBBuscar.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "Double Click DGV"
    Private Sub DGVDados_CellContentDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellContentDoubleClick
        objBLL = Nothing
        If (e.RowIndex >= 0) Then
            objBLL = New FormasPgtoBLL(Val(DGVDados.Rows(e.RowIndex).Cells(0).Value))
        End If
    End Sub
#End Region
#Region "Eventos do ComboBox"
    Private Sub CBFiltro_SelectedIndexChanged_1(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        TXTLocalizar.Clear()
        If (CBFiltro.SelectedIndex = 3) Then
            TXTLocalizar.Visible = False

        Else
            TXTLocalizar.Clear()
            TXTLocalizar.Visible = True
            CBFiltro.Visible = True
        End If
        If TXTLocalizar.Text = "" Then
            CarregaGrid()
        End If

    End Sub
#End Region
#Region "Excluir"
    Private Sub TSBExcluir_Click(sender As System.Object, e As System.EventArgs) Handles TSBExcluir.Click
        Try
            Dim cod As Integer = 0
            Dim indice As Integer = -1
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("pag_codigo") Is Nothing) Then
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                TSBExcluir.BackColor = Color.Gainsboro

                If MessageBox.Show("Confirma exclusão da Forma de Pagamento? " & DGVDados.Rows(indice).Cells("pag_descricao").Value.ToString() & " ?", "", MessageBoxButtons.YesNo) = vbYes Then
                    cod = DGVDados.Rows(indice).Cells("pag_codigo").Value.ToString()
                    objBLL.Excluir(cod)
                    MsgBox("Exclusão efetuada com sucesso!", MsgBoxStyle.Information)
                    CarregaGrid()
                End If
            Else
                MessageBox.Show("Selecione o Forma de Pagamento a ser realizado á excluido", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            TSBExcluir.BackColor = Color.Transparent
        End Try
    End Sub
#End Region
#Region "Key Press"
    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub
#End Region
#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
#Region "Detalhes"
    Private Sub TSBDetalhes_Click(sender As System.Object, e As System.EventArgs) Handles TSBDetalhes.Click
        For Each linha As DataGridViewRow In DGVDados.SelectedRows
            If (Not linha.Cells("pag_codigo") Is Nothing) Then
                objBLL = New FormasPgtoBLL(Val(DGVDados.Rows(linha.Index).Cells("pag_codigo").Value.ToString()))
            End If
        Next
        If (Not IsNothing(objBLL)) Then
            If (objBLL.pagcodigo > 0) Then gerarelatrio(objBLL.pagdescricao, "{Gen_FormaPagamento.pag_codigo}=" & objBLL.pagcodigo)
        End If
    End Sub
    Public Sub gerarelatrio(titulo As String, formula As String)
        Try
            Dim frmrecursos As New RelatoriosfinFRM
            Dim rpt As New FormasPagamentoREL

            Try
                rpt.SetDatabaseLogon(usuario, senha)
                rpt.SetParameterValue(0, titulo)
                rpt.RecordSelectionFormula = formula
                frmrecursos.CrystalReportViewer2.ShowGroupTreeButton = False
                frmrecursos.CrystalReportViewer2.ReportSource = rpt
                frmrecursos.ShowDialog()
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            frmrecursos.Dispose()
            rpt = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Finally

        End Try
    End Sub
#End Region
End Class
